<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	会员组管理
	会员组的增删查改
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$uptypes=array(
	'1'=>'总订单数',
	'2'=>'订单总金额'
);
if($_RQ['op']=='list'){
	$setting=uni_setting('member_up');
	if(!$setting){
		exi('请先设置会员等级升级依据！','error','set');
	}
	$up_title=$uptypes[$setting['uptype']];
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	//会员组名称
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['title']=$_RQ['title'];
	}
	$orderby='level asc';
	$list=pdo_getslice('core_member_groups',$params,array($pindex,$psize),$total,array('id','title','level','uptype','upneed','createtime'),'',$orderby);
	if($list){
		
		foreach($list as &$item){
			$item['createtime']=date('20y/m/d H:i:s',$item['createtime']);
			$item['up_desc']=$up_title.'达到'.$item['upneed'];
			$item['members']=pdo_count('core_members',array('groupid'=>$item['id']));
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'会员等级列表'),
			array('op'=>'edit','title'=>'添加会员等级'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'等级名称','type'=>'string'),
			array('name'=>'level','title'=>'等级权重','type'=>'string'),
			array('name'=>'up_desc','title'=>'升级条件','type'=>'string'),
			array('name'=>'members','title'=>'包含会员人数','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
		)),
		'search_one'=>array('name'=>'title'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$setting=uni_setting('member_up');
	if(!$setting){
		exi('请先设置会员等级升级依据！','error','set');
	}
	$up_title=$uptypes[$setting['uptype']];
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_member_groups',array('id'=>$id));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'等级',
		'tabs'=>array(
			array('op'=>'list','title'=>'会员等级列表'),
			array('op'=>'edit','title'=>'添加会员等级'),
		),
		'edit'=>array(
			array('type'=>'string','title'=>'等级名称','name'=>'title'),
			array('type'=>'string','title'=>'等级权重','name'=>'level', 'help'=>'填写小于100的正整数。数值越大，权重越高。高权重等级会员自动获得低权重等级会员特权。</dr>不同会员等级权重值不得重复！' ),
			array('type'=>'string','title'=>$up_title,'name'=>'upneed','help'=>"用户升级到该等级需要的{$up_title}" ),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$data=array(
		'level'=>intval($post['level']),
		'upneed'=>intval($post['upneed']),
		'title'=>trim($post['title']),
	);
	if(!$data['level']){
		exi('会员等级权重未设置或者不合法！','error');
	}
	$id=intval($post['id']);
	$check=pdo_getcolumn('core_member_groups',array('uniacid'=>$_SESSION['uniacid'],'level'=>$data['level']),'id');
	if($check && $check!=$id){
		exi('会员等级权重重复,请重新设置！','error');
	}
	if($id){
		pdo_update('core_member_groups',$data,array('id'=>$id));
		exi('修改成功！','','goback');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('core_member_groups',$data);
		exi('新增成功！','','list');
	}
}
if($_RQ['op']=='del'){
	//删除会员等级
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_member_groups',array('id'=>$id));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_member_groups',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
/*
	会员升级依据
*/
if($_RQ['op']=='set'){
	if($_RQ['postdata']){
		$post=get_postdata();
		uni_setting('member_up',$post);
		exi('设置成功！','','goback');
	}
	$item=uni_setting('member_up');
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'radio','title'=>'升级依据','name'=>'uptype','options'=>'uptypes','help'=>'会员升级依据' ),
		),
	);
	$return['uptypes']=$uptypes;
	exi($return);
}